/*************************************************************************
*
*	simulate_current.c
*
*  Copyright 2007,2008,2009 Steve Stancliff
*
*  Part of the TAOSF SSA system.
*
*
*  This module provides functions for simulating water current
*
*
**************************************************************************
*
*  version 1.0.4, 4/24/09, SBS
*   - documentation
*
*  version 1.0.3, 10/4/08, SBS
*   - documentation
*
*  version 1.0.2, 8/27/08, SBS
*   - use status_SetTimeString()
*
*  version 1.0.1, 5/30/08, SBS
*    - move conversions (lat, long, deg, rad, etc) into conversions.h
*    - move time format into status.h
*
*  version 1.0, 5/17/08, SBS
*    - split out from simulate_science.c
*
**************************************************************************
*
*  This program is free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.
*
*  This program is distributed in the hope that it will be useful, but 
*  WITHOUT ANY WARRANTY; without even the implied warranty of 
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
*  General Public License for more details.
*
*  You should have received a copy of the GNU General Public License 
*  along with this program; if not, write to the Free Software Foundation, 
*  Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 
*
**************************************************************************/
/**
*  @file 
*  @ingroup plat_sci_simulate
*  @brief
*  This module provides functions for simulating water current
*  <hr>
**/


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <math.h>

#include "waypoints.h"
#include "platform_types.h"
#include "platform_modes.h"
#include "status.h"
//#include "logfile.h"
#include "conversions.h"

#include "simulate_current.h"



//  constant current velocity until somebody codes a model
#define CURRENT_SPEED_LAT_MPS	(-0.43)
#define CURRENT_SPEED_LON_MPS	(-0.43)


/**
***************************************************************************
*
* simulate_current()
*/
/** 
*
* @brief
* Returns a vector representing the drift in m/s
*
*
**************************************************************************/
static VELOCITY simulate_current(
			POSITION p
			, time_t time)
{
	VELOCITY rval;
	
	rval.dLatMps = (CURRENT_SPEED_LAT_MPS);
	rval.dLonMps = (CURRENT_SPEED_LON_MPS);

//	printf("current velocity = (%f, %f)\n",rval.dLatMps,rval.dLonMps);
	
	return(rval);
}


/**
***************************************************************************
*
* simulate_CurrentSpeed()
*/
/** 
*
* @brief
*
*
**************************************************************************/
SCI_DBL simulate_CurrentSpeed(
		POSITION p
		, time_t t)
{
	struct tm *timeinfo;	
	SCI_DBL sd;	
	VELOCITY c;

//	static int memory = 0;
	
//	if(memory == 0){
	
		sd = status_InitSciDbl();
//	}
	
	c = simulate_current(p,t);
		
	sd.dValue = sqrt(c.dLatMps*c.dLatMps + c.dLonMps*c.dLonMps);	

	strncpy(sd.sUnits,"m/s", UNITSTRLEN);

	sd.position = p;
	
	timeinfo = localtime(&t);
//	strftime(sd.sTime, sizeof(sd.sTime), TIMEFORMAT, timeinfo);

	status_SetTimeString(sd.sTime, sizeof(sd.sTime),timeinfo);

//	printf("current speed = %f\n",sd.dValue);

	return(sd);
}


/**
***************************************************************************
*
* simulate_CurrentAngle()
*/
/** 
*
* @brief
*
*
**************************************************************************/
SCI_DBL simulate_CurrentAngle(
		POSITION p
		, time_t t)
{
//	static int memory=0;
	
	struct tm *timeinfo;	
	VELOCITY c;
	SCI_DBL sd;	

//	if(memory == 0){
//	
		sd = status_InitSciDbl();
//	}

	c = simulate_current(p,t);
		
	sd.dValue = DEGREES(atan2(c.dLonMps, c.dLatMps));
	
	while(sd.dValue > 360){
		sd.dValue -= 360;
	}

	while(sd.dValue < 0){
		sd.dValue += 360;
	}
		
	strncpy(sd.sUnits,"degrees", UNITSTRLEN);
	
	sd.position = p;
	
	timeinfo = localtime(&t);
//	strftime(sd.sTime, sizeof(sd.sTime), TIMEFORMAT, timeinfo);

	status_SetTimeString(sd.sTime, sizeof(sd.sTime),timeinfo);

//	printf("current angle = %f\n",sd.dValue);

	return(sd);
}


